#include "sys.h"
#include "user_cfg.h"


//void usercfg_init(void);
//void usercfg_set_default(void);
//void usercfg_handle_loop(void);
//void user_reboot(unsigned char nsec);

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h> 
#include <sys/mman.h>

#include "nstar_share.h"

static unsigned char macaddr[6] = {0x00, 0xe0};
unsigned int make_cpuid(void)
{
#define CPU_Sid_Reg (0x01c23800)
#define Page_Size (4096*1)    
    int mem_fd;
    volatile unsigned int *base_map, *reg_addr;
    unsigned int base, off, id_u32;
    unsigned char id[16];
    if ((mem_fd = open("/dev/mem", O_RDWR)) < 0) {
        printf("open error\r\n");
        exit(-1);
    }
    //映射区长度必须是“内存页的整数倍”
    base = CPU_Sid_Reg & ((0-Page_Size));
    off = CPU_Sid_Reg- base;
    //myargx(base); myargx(off);
    if ((base_map = (unsigned int *)mmap(NULL, Page_Size, PROT_READ, MAP_SHARED, mem_fd, base)) == NULL)
    {
        printf("mmap error\r\n");
        close(mem_fd);
        exit(-1);
    }
    close(mem_fd);                              
    reg_addr = (unsigned int)base_map + off; 
    memcpy(id, (unsigned char*)reg_addr, 16);
    munmap(base_map, Page_Size);
    id_u32 = nstar_crc32_ts(id, 16);
    /*copy cpuid to mac @ bytes 2-5*/
    memcpy(macaddr+2, (unsigned char*)&id_u32, 4);
    return id_u32;
}

void make_serid_5bytes(unsigned char *dest ,unsigned char len) {
    if(len <= 5){
    	int i;
    	char tmpbuf[20];
    	unsigned char *id = macaddr;
    	unsigned int tmp= (id[2] & 0xf)<<24 | id[3]<<16 | id[4]<<8 | id[5] ;
    	sprintf(tmpbuf, "%010d", tmp);
    	printf("chip nb is %s\n", tmpbuf);
    	for(i=0;i<5;i++){
    		(id[i] = nstar_cmm_canver_get_hex_dec_byte((unsigned char*)tmpbuf+ i*2));
    	}
    	memcpy(dest, id+ 5-len, len);
    }
}


void board_init(void)
{
   // app_delay_test();
    usercfg_init();
    board_ch_init();
    //HCF4051_OPEN_MP3(0);
    mp3_play_init();
    
	//printf("board init ok %x \r\n", make_cpuid());
}


void board_cmm_setvol(unsigned short vol) {


}



